//引入请求的方法
import * as api from "@/api/index";
export default {
  //开启命名空间
  namespaced: true,
  state: {
    ProductData: [],
  },
  mutations: {
    //更该productData
    SETPRODUCTDATA(state, payload) {
      state.ProductData = payload;
    },
    //让库存减少
    DECREASE_INVERNTORY(state,payload){
       let target = state.ProductData.find(item=>item.id===payload);
       if(target){
        target.inventory--;
       }
    },
    //让库存再加回去
    // inventory
    SETINVENTORY(state,payload){
         //判断ProductData中数据
         let target = state.ProductData.find(item => item.id === payload.id)
         if(target){
          target.inventory++
         }
    }
  },
  getters: {},
  actions: {
    //不在action里调用vuex的插件会报错
    async FETCH_PRODUCT(context) {
      let res = await api.fetchProduct();
      context.commit("SETPRODUCTDATA",res.data);
    },
  },
};
